package org.eclipse.mobileapplicationdeveloper.base.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.eclipse.mobileapplicationdeveloper.base.command.Consts;

/**
 * Utils class.
 * @author Pablo Oyarzabal
 *
 */
public class UtilCommands {

	private static final int BUFFER = 1024;

	/**
	 * Replace a string in a file.
	 * 
	 * @param oldValue
	 * @param newValue
	 * @param pathDestination
	 * @param fileName
	 */
	public static void replaceStringInFile(String oldValue, String newValue,
			String pathDestination, String fileName) {
		try {
			File file = new File(pathDestination + "/" + fileName);
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = "", oldtext = "";
			while ((line = reader.readLine()) != null) {
				oldtext += line + "\r\n";
			}
			reader.close();
			// replace a word in a file
			// String newtext = oldtext.replaceAll("drink", "Love");

			// To replace a line in a file
			String newtext = oldtext.replaceAll(oldValue, newValue);

			System.setProperty( "file.encoding", "UTF-8" );
			
			FileOutputStream fileStream = new FileOutputStream(new File(pathDestination + "/" + fileName));
			OutputStreamWriter writer = new OutputStreamWriter(fileStream, "UTF-8");
			 
//		    Charset utf8 = Charset.forName("UTF-8");
//		    String original = "abc\u00A3";
//		    byte[] encoded = original.getBytes(utf8);
//		    // create mock file input stream
//		    InputStream stream = new ByteArrayInputStream(encoded);
//		    // decode the data
//		    String decoded = decodeFromStream(stream, utf8);
		    
//			Writer out = new BufferedWriter(writer);
			writer.write(newtext);
		 
			writer.flush();
			writer.close();
//			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathDestination + "/" + fileName),"UTF8"));
						
//			FileWriter writer = new FileWriter(pathDestination + "/" + fileName);
//			
//			writer.write(newtext);
//			writer.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

	}

	/**
	 * Rename a folder.
	 * 
	 * @param oldPath
	 * @param newPath
	 */
	public static boolean renameFolder(String oldPath, String newPath) {
		File file = new File(oldPath);
		File newFile = new File(newPath);
		// Renames the file denoted by this abstract pathname.
		boolean renamed = file.renameTo(newFile);
		if (renamed) {
			System.out.println("File renamed to " + newFile.getPath());
		} else {
			System.out.println("Error renaming file " + file.getPath());
		}
		return renamed;
	}



	/**
	 * Command for to decompress zip files.
	 * 
	 * @param fileZipName
	 * @param pathDestination
	 */
	public static void unZipFileCommand(String fileZipName,
			String pathDestination) {

		String zipFile = fileZipName; // "C:\\tmp\\JustExample.zip";
		String targetPath = pathDestination; // "C:\\tmp\\JustExampleFolder2";

		System.out.format("Extracting {0} ...", zipFile);
		System.out.format("...target directory: {0}", targetPath);
		File path = new File(targetPath);
		path.delete();
		path.mkdirs();

		BufferedOutputStream dest = null;
		FileInputStream fis;
		try {

			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			InputStream resourceStream = loader.getResourceAsStream(zipFile);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					resourceStream);
			ZipInputStream zis = new ZipInputStream(bufferedInputStream);
			ZipEntry entry;
			try {
				while ((entry = zis.getNextEntry()) != null) {
					if (entry.isDirectory()) {
						File f = new File(targetPath + "/" + entry.getName());
						f.mkdirs();
						continue;
					}
					int count;
					byte data[] = new byte[BUFFER];

					// write the files to the disk
					FileOutputStream fos = new FileOutputStream(targetPath
							+ "/" + entry.getName());
					dest = new BufferedOutputStream(fos, BUFFER);
					while ((count = zis.read(data, 0, BUFFER)) != -1) {
						dest.write(data, 0, count);
					}
					dest.flush();
					dest.close();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			zis.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


}
